package com.zkt.practice.server.utils;

import lombok.extern.slf4j.Slf4j;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @author zkt
 */

@Slf4j
public class DateUtils {
	
	/**
	 * 字符串转时间
	 */
	public static LocalDateTime parseStrToDate(String timestamp) {
		try {
			DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
			return LocalDateTime.parse(timestamp, formatter);
		} catch (Exception e) {
			log.error("parseDate解析：{}，异常：{}", timestamp, e.getMessage(), e);
			return null;
		}
	}
	
	/**
	 * 日期格式化
	 */
	public static String format(LocalDate date, String format) {
		try {
			if (date == null) {
				return null;
			}
			
			DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
			
			return date.format(formatter);
		} catch (Exception e) {
			log.error("日期格式化失败{}", e.getMessage(), e);
		}
		return null;
	}
	
}
